<?php

class Application_Model_AuthManager
{
    /**
     * @return Application_Model_AuthManager
     */
    public static function getInstance()
    {
        return new self();
    }

    protected function  __construct()
    {
    }

    /**
     * @param string $userName
     * @param string $pass
     * @return boolean
     */
    public function authorizedUser($userName, $pass)
    {
        $authorizedUser = false;
        $adapter = $this->getAuthAdapter();
        $adapter->setIdentity($userName);
        $adapter->setCredential($pass);

        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($adapter);
        if ($result->isValid())
        {
            $user = $adapter->getResultRowObject();
            if ('ACTIVE' == $user->status)
            {
                $auth->getStorage()->write($user);
                $authorizedUser = true;
                My_Logger::getInstance()->info('Access granted to user "%s".', $userName);
            }
            else
            {
                My_Logger::getInstance()->info('Access denied to user "%s".', $userName);
            }
        }
        else
        {
            My_Logger::getInstance()->info('User "%s" not alowed.', $userName);
        }
        return $authorizedUser;
    }

    /**
     * @return Zend_Auth_Adapter_DbTable
     */
    protected function getAuthAdapter()
    {
        $dbAdapter = Zend_Db_Table::getDefaultAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
        $authAdapter->setTableName('user')
            ->setIdentityColumn('user')
            ->setCredentialColumn('password');
        return $authAdapter;
    }


}